package com.sso.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sso.entity.User;
import com.sso.form.UserForm;
import com.sso.service.UserService;
import com.sso.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xia
 * @since 2022-07-23
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Value("${server.port}")
    String port;
    @ApiOperation("登录接口")
    @PostMapping("/login")
    public R login(String username, String password){
        MultiValueMap<String,Object> paramsMap=new LinkedMultiValueMap<>();
        paramsMap.set("username",username);
        paramsMap.set("password",password);
        paramsMap.set("grant_type","password");
        paramsMap.set("client_id","client");
        paramsMap.set("client_secret","123456");
        RestTemplate restTemplate=new RestTemplate();
//        restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(map.get("client_id").toString(),map.get("client_secret").toString()));
        OAuth2AccessToken token=restTemplate.postForObject("http://localhost:"+port+"/oauth/token",paramsMap,OAuth2AccessToken.class);
        if(token.getValue()==null){
            return R.error(Integer.parseInt(token.getAdditionalInformation().get("code").toString()),token.getAdditionalInformation().get("msg").toString());
        }else{
            String tokenType = "Bearer";
            String tokenValue=token.getValue();
            String token1=tokenType+" "+tokenValue;
            return R.ok().put("token",token1);
        }
    }
    @ApiOperation("注册接口")
    @PostMapping("/add")
    public R add(UserForm userForm) {
        User user=new User();
        User one = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, userForm.getUsername()));
        if(one!=null){
            return R.error("该用户名已存在");
        }
        BeanUtils.copyProperties(userForm,user);
        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
        userService.save(user);
        return R.ok();
    }
    @PostMapping("/get")
    public User get(String name) {
        User one = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, name));
        System.out.println(one);
        return one;
    }
}
